<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Demo VUE</title>
</head>
<body>
    <div id="app">
        <!-- 子组件 -->
        <hello-world :zimsg="msg" @change="toChange"></hello-world>
    </div>
    <!-- 子组件模版 -->
    <script type="text/template" id="hello">
        <div>
            <span>{{zimsg}}</span>
            <button @click="toChange()">非中间件</button>
            <button @click="toChange2()">中间件</button>
        </div>
    </script>
</body>
<script src="https://cdn.bootcss.com/vue/2.5.17-beta.0/vue.min.js"></script>
<script>
    //中间件
    var eventHub = new Vue();
    // 注册组件
    Vue.component('helloWorld', {
        template:"#hello",
        props:{
            zimsg:{
                type:String
            }
        },
        data:{
            zimsg:this.msg,
        },
        created:function(){
            console.log(this.zimsg);
        },
        methods:{
            toChange(){
                this.$emit("change","byebye")
            },
            toChange2(){
                eventHub.$emit("change2","goodbye")
            }
        }
    })
    //父组件
    var app = new Vue({
        el:"#app",
        data:{
            msg:"hello word!"
        },
        created:function(){
            var self =this;

            eventHub.$on("change2",function(msg){
                console.log(msg)
                self.msg=msg;
            })
        },
        methods:{
            toChange(msg){
                this.msg=msg;
            }
        }
    })
</script>
</html>